using System.Collections.Generic;
using FireStarter.CodeGeneration.Generator;
using FireStarter.CodeGeneration.Parser;
using NUnit.Framework;

namespace FireStarter.Test.CodeGeneration.Parser
{
    [TestFixture]
    public class TestStatementParser
    {
        private static readonly List<IActionParser> _defaultActionParsers = new List<IActionParser> 
                { new ValueExpressionParser(), new ForEachExpressionParser() };

        [Test]
        public void Test_EmptyExpressionWithinLiterals()
        {
            //---------------Set up test pack-------------------
            const string contentBefore = "content before";
            const string contentAfter = "content after";
            const string masterContent = contentBefore + "<<| |>>" + contentAfter;
            string content = masterContent;
            IParser parser = new StatementParser();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            TemplateExpression expression = parser.Parse(ref content, _defaultActionParsers);
            //---------------Test Result -----------------------
            ParserTestUtils.AssertExpressionIsLiteral(contentBefore, expression);
            ParserTestUtils.AssertExpressionIsLiteral("", expression.Next);
            ParserTestUtils.AssertExpressionIsLiteral(contentAfter, expression.Next.Next);
            Assert.AreEqual("", content, "Template content should get fully consumed");
        }

        [Test]
        public void Test_EmptyStreamCreatesNullExpression()
        {
            //---------------Set up test pack-------------------
            IParser parser = new StatementParser();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            string content = "";
            TemplateExpression expression = parser.Parse(ref content, _defaultActionParsers);
            //---------------Test Result -----------------------
            Assert.IsNull(expression);
        }


        [Test]
        public void Test_ExpressionOnly()
        {
            //---------------Set up test pack-------------------
            const string masterContent = "<<| |>>";
            string content = masterContent;
            IParser parser = new StatementParser();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            TemplateExpression expression = parser.Parse(ref content, _defaultActionParsers);
            //---------------Test Result -----------------------
            ParserTestUtils.AssertExpressionIsLiteral("", expression);
            Assert.IsNull(expression.Next);
            Assert.AreEqual("", content, "Template content should get fully consumed");
        }

        [Test]
        public void Test_LiteralOnly()
        {
            //---------------Set up test pack-------------------
            const string masterContent = "bob";
            string content = masterContent;
            IParser parser = new StatementParser();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            TemplateExpression expression = parser.Parse(ref content, _defaultActionParsers);
            //---------------Test Result -----------------------
            ParserTestUtils.AssertExpressionIsLiteral(masterContent, expression);
            Assert.AreEqual("", content, "Template content should get fully consumed");
        }

        //private MemoryStream GetStreamForString(string content)
        //{
        //    MemoryStream contentStream = new MemoryStream();
        //    StreamWriter writer = new StreamWriter(contentStream);
        //    writer.Write(content);
        //    writer.Flush();
        //    contentStream.Seek(0, SeekOrigin.Begin);
        //    return contentStream;
        //}
    }
}